<?php
/**
 * Created by PhpStorm.
 * User: phc
 * Date: 19-8-15
 * Time: 下午5:38
 */

namespace app\common\model;

use think\Model;

class CommonModel extends Model
{
    /**
     * 获取数据行数
     *
     * @param $table
     * @return float|string
     */
    protected function getCount($table)
    {
        return $result = db($table)
            ->where('enable', '=', 1)
            ->count();
    }

    /**
     * 伪删除数据
     *
     * @param $table
     * @param $arrayId
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    protected function deleteByIds($table, $arrayId)
    {
        return $result = db($table)
            ->where('id', 'in', $arrayId)
            ->update(["enable" => 0, "disabletime" => date("Y-m-d H:i:s")]);
    }

    /**
     * 获取数据
     *
     * @param $table
     * @param $id
     * @return array|\PDOStatement|string|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function getById($table, $id)
    {
        return $result = db($table)
            ->where(["id" => $id, "enable" => 1])
            ->find();
    }

    /**
     * 修改数据
     *
     * @param $table
     * @param $param
     * @return bool|int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    protected function updateById($table, $param)
    {
        $id = 0;
        if (!array_key_exists("id", $param))
            return false;
        else {
            $id = $param["id"];
            unset($param["id"]);
        }
        return $result = db($table)
            ->where("id", $id)
            ->update($param);
    }

    /**
     * 添加数据
     *
     * @param $table
     * @param $param
     * @return int|string
     */
    protected function add($table, $param)
    {
        if (!array_key_exists("id", $param))
            unset($param["id"]);
        return $result = db($table)
            ->insertGetId($param);
    }

    /**
     * 批量添加数据
     *
     * @param $table
     * @param $params 二维数组
     * @return int|string
     */
    protected function adds($table, $params)
    {
        return $result = db($table)
            ->insertAll($params);
    }

    /**
     * 获取图片信息
     *
     * @param $table
     * @param $tableId
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function getImagesByTableId($table, $tableId)
    {
        return $result = db('zbc_images')
            ->where([
                "tablename" => $table,
                "tableid" => $tableId,
                "enable" => 1
            ])
            ->select();
    }

    /**
     * 添加图片(支持批量)
     *
     * @param $tableName
     * @param $imgs
     * @param $tableId
     * @return bool|int|string
     */
    protected function addImages($tableName, $imgs, $tableId)
    {
        $params = [];
        if (!$tableId)
            return false;
        if (!is_array($imgs)) {
            $params = [
                ['phy' => $imgs, 'tableid' => $tableId, 'tablename' => $tableName]
            ];
        } else if (count($imgs) == count($imgs, 1)) {
            $imgs['tableid'] = $tableId;
            $imgs['tablename'] = $tableName;
            $params[0] = $imgs;
        } else if (count($imgs) > 1) {
            //批量
            foreach ($imgs as $key => $val) {
                $img = $val;
                $img['tableid'] = $tableId;
                $img['tablename'] = $tableName;
                array_push($params, $img);
            }
        } else
            return false;
        return $this->adds('zbc_images', $params);
    }

    /**
     * 伪删除图片
     *
     * @param $tableName
     * @param $tableId 数组或逗号分割的字符串
     * @return bool
     */
    protected function deleteImagesByTableId($tableName, $tableId)
    {
        return $result = db('zbc_images')
            ->where('tablename', $tableName)
            ->where('tableid', 'in', $tableId)
            ->update([
                'enable' => 0,
                'disabletime' => date('Y-m-d H:i:s')
            ]);
    }

}